package org.openkoala.koala.popup.javaclass.crud;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.openkoala.koala.deploy.curd.module.ui.model.EntityViewUI;

public class CrudConfigurationDialog extends Dialog {

	private EntityViewUI entityViewUI;
	
	private CrudAction action;

	private CrudTabControl listControl;
	private CrudTabControl queryControl;
	private CrudTabControl addControl;
	private CrudTabControl updateControl;
	private CrudTabControl viewControl;
	
	protected CrudConfigurationDialog(Shell shell, EntityViewUI entityViewUI, CrudAction action) {
		super(shell);
		this.entityViewUI = entityViewUI;
		this.action = action;
	}

	@Override
	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setText("CRUD配置");
	}
	
	@Override
	protected Control createDialogArea(Composite parent) {
		Composite composite = new Composite(parent, SWT.NONE);
		initializeDialogUnits(composite);

		GridData mainGridData = new GridData(550, 450);
		composite.setLayoutData(mainGridData);

		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 1;
		composite.setLayout(gridLayout);
		
		createTabs(composite);

		return parent;
	}

	private void createTabs(Composite parent) {
        GridData gridData = new GridData(GridData.FILL_BOTH);

        TabFolder tabFolder = new TabFolder(parent, SWT.NONE);
	    tabFolder.setLayoutData(gridData);
	    tabFolder.setData(entityViewUI);
        
        TabItem gridTab = new TabItem(tabFolder, SWT.NONE);
        gridTab.setText("列表页面");
        
	    TabItem queryTab = new TabItem(tabFolder, SWT.NONE);
	    queryTab.setText("查询条件");
        
        TabItem viewTab = new TabItem(tabFolder, SWT.NONE);
        viewTab.setText("详情页面");
        
        TabItem addTab = new TabItem(tabFolder, SWT.NONE);
        addTab.setText("新增页面");
        
        TabItem updateTab = new TabItem(tabFolder, SWT.NONE);
        updateTab.setText("修改页面");
        
        listControl = new CrudTabControl(tabFolder, entityViewUI.getListModel(), entityViewUI.getEntityModel(), this);
        gridTab.setControl(listControl.getControl());
       
        queryControl = new CrudTabControl(tabFolder, entityViewUI.getQueryModel(), entityViewUI.getEntityModel(), this);
        queryTab.setControl(queryControl.getControl());
        
        addControl = new CrudTabControl(tabFolder, entityViewUI.getAddModel(), entityViewUI.getEntityModel(), this);
        addTab.setControl(addControl.getControl());
        
        updateControl = new CrudTabControl(tabFolder, entityViewUI.getUpdateModel(), entityViewUI.getEntityModel(), this);
        updateTab.setControl(updateControl.getControl());
        
        viewControl = new CrudTabControl(tabFolder, entityViewUI.getViewModel(), entityViewUI.getEntityModel(), this);
        viewTab.setControl(viewControl.getControl());
	}

	public void refressAll() {
		listControl.refreshData();
		queryControl.refreshData();
		addControl.refreshData();
		updateControl.refreshData();
		viewControl.refreshData();
	}
	
	@Override
	protected void okPressed() {
		action.showGenerateFilesList(getShell(), entityViewUI);
		super.okPressed();
	}
	
}
